package org.tissou.client.Panel_Premier;

import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import org.tissou.client.Interface.Base_Panel;
import org.tissou.client.Services.*;
import org.tissou.client.widgets.Label_MessageInformation;


/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 20-Mar-2009
 * Time: 22:27:40
 * To change this template use File | Settings | File Templates.
 */
public class Panel_PrePremier extends Base_Panel {

  final DockPanel principal = new DockPanel();

  Label tissou = new Label("Tissou");

  DockPanel panel_proprietaireexistant = new DockPanel();
  Label texte = new Label("Qui es-tu?");
  ArbreDeProprietaires arbre = new ArbreDeProprietaires();
  Label passLabel = new Label("Les chiens aboient, la caravane passe");
  Label texte2 = new Label("Ton mot de passe:");
  PasswordTextBox pass = new PasswordTextBox();
  Button ok = new Button ("Oui Oui c'est bien moi");

  DockPanel panel_nouveauproprietaire = new DockPanel();
  Label nouveau = new Label("Je suis un nouveau proprietaire");
  Label nouveau_label = new Label("Entre ton nom :");
  TextBox nouveau_nom = new TextBox();
  Button nouveau_ok = new Button("OK");
  Button nouveau_annuler = new Button("Annuler");

  Label_MessageInformation message_information = new Label_MessageInformation();

   protected void MiseEnPlaceStyles() {
     panel_proprietaireexistant.addStyleName("dockPanelEncadre");
     panel_nouveauproprietaire.addStyleName("dockPanelEncadre");
     tissou.setStylePrimaryName("gwt-Label-tissou");
     texte.setStylePrimaryName("gwt-Label-bold");
     texte2.setStylePrimaryName("gwt-Label-bold");
     nouveau.setStylePrimaryName("gwt-Label-bold");
     
                    
  }

  protected void MiseEnPlaceParametresWidgets() {
  }

  protected void MiseEnPlaceParametresPanel() {
    
    panel_proprietaireexistant.add(texte, DockPanel.NORTH);
    panel_proprietaireexistant.add(arbre, DockPanel.NORTH);
    panel_proprietaireexistant.add(passLabel, DockPanel.NORTH);
    panel_proprietaireexistant.add(texte2, DockPanel.NORTH);
    panel_proprietaireexistant.add(pass, DockPanel.NORTH);
    panel_proprietaireexistant.add(ok, DockPanel.NORTH);

    panel_nouveauproprietaire.add(nouveau, DockPanel.NORTH);
    panel_nouveauproprietaire.add(nouveau_label, DockPanel.NORTH);
    panel_nouveauproprietaire.add(nouveau_nom, DockPanel.NORTH);
    panel_nouveauproprietaire.add(nouveau_ok, DockPanel.WEST);
    panel_nouveauproprietaire.add(nouveau_annuler, DockPanel.EAST);

  }

  protected void MiseEnPlaceActions() {
    ok.addClickHandler(new ClickHandler(){

       @Override
        public void onClick(ClickEvent event) {
          try {InformationsGlobales.personneLoguee = arbre.donneLeProprietaireSelectionne();
          } catch (MonException e){
            message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE, e.getMessage());
            throw e;
          }

        AsyncCallback<Boolean> cb = new AsyncCallback<Boolean>() {

          @Override
          public void onFailure(Throwable throwable) {
            message_information.setMessage(Label_MessageInformation.Etat.SERVICEPASMARCHE,"Argghh.");
          }

          @Override
          public void onSuccess(Boolean authentifie) {
            if (authentifie) {
              RootPanel.get().remove(principal);
              Panel_Premier pp = new Panel_Premier();
              pp.afficherDans(RootPanel.get());
            } else {
              message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE,"Trop pas mec, C'est pas le bon mot de passe ca mec.");
            }
          }
        };
        if (pass.getText().equals("")) {
          message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE,"Trop pas mec, C'est pas le bon mot de passe ca mec.");
        } else {
          AuthentificationService.App.getInstance().authentifier(InformationsGlobales.personneLoguee.getNom(), pass.getText(), cb);
          message_information.setMessage(Label_MessageInformation.Etat.SERVICEATTENTE, "En attente de l'authentification du serveur.");
        }
      }
    });

    nouveau_annuler.addClickHandler(new ClickHandler(){

        @Override
        public void onClick(ClickEvent event) {
             nouveau_nom.setText("");
        }
    });

    nouveau_ok.addClickHandler(new ClickHandler(){

        @Override
        public void onClick(ClickEvent event) {
            if (nouveau_nom.getText().equals("")) {
              message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE,"Aucun nom n'a ete rentre");
            }
            else {
              final AsyncCallback c = new AsyncCallback(){
                public void onFailure(Throwable caught) {
                  System.out.println("Creer Proprietaire: Ca a pas marche pas de chance");
                  message_information.setMessage(Label_MessageInformation.Etat.SERVICEPASMARCHE,caught.getLocalizedMessage());
                }

                public void onSuccess(Object result) {
                  System.out.println("Creer Proprietaire: Congratulations Petite Clairette, You are the best of the world !");
                  arbre.Refresh();
                  message_information.setMessage(Label_MessageInformation.Etat.SERVICEMARCHE,"C'est bon le proprietaire a bien ete cree");
                }
              };
              NouveauProprioService.App.getInstance().CreerNouveauProprietaire(nouveau_nom.getText(), c);
              message_information.setMessage(Label_MessageInformation.Etat.SERVICEATTENTE, "En attente d'une reponse du serveur.");
            }
        }
    });
  }

  protected void MiseEnPlacePanels() {
     principal.add(tissou, DockPanel.NORTH);
    principal.setCellHorizontalAlignment(tissou,HasHorizontalAlignment.ALIGN_CENTER);

    principal.add(message_information, DockPanel.NORTH);
    principal.setCellHeight(message_information, "40px");
    principal.setCellHorizontalAlignment(message_information,HasHorizontalAlignment.ALIGN_CENTER);

    principal.add(panel_proprietaireexistant, DockPanel.WEST);

    principal.add(panel_nouveauproprietaire, DockPanel.WEST);
    principal.setCellHorizontalAlignment(panel_proprietaireexistant, HasHorizontalAlignment.ALIGN_CENTER);
  }

  public Panel donneLePanelPrincipal() {
    return principal;
  }
}
